using System;
using System.Collections.Generic;
using System.Text;

namespace Acms.AcmsControls
{
    public abstract class TemplateLoaderPage : System.Web.UI.Page
    {
        protected override void OnPreInit(EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["AcmsPostId"]))
            {
                try
                {
                    postId = int.Parse(Request.QueryString["AcmsPostId"]);
                }
                catch
                { }
            }

            if (!string.IsNullOrEmpty(Request.QueryString["AcmsTemplatePath"]))
            {
                templateUrl = Request.QueryString["AcmsTemplatePath"];
            }

            if (!string.IsNullOrEmpty(Request.QueryString["AcmsMasterPagePath"]))
            {
                this.MasterPageFile = Request.QueryString["AcmsMasterPagePath"];
            }

            if (!string.IsNullOrEmpty(Request.QueryString["AcmsTheme"]))
            {
                this.Theme = Request.QueryString["AcmsTheme"];
            }

            base.OnPreInit(e);
        }

        private int postId;
        private string templateUrl;
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            EnsureChildControls();
        }

        protected override void CreateChildControls()
        {
            TemplateLoaderControl tmp = GetLoaderControl();
            tmp.PostId = postId;
            tmp.PageTemplateUrl = templateUrl;
        }

        protected abstract TemplateLoaderControl GetLoaderControl();

        protected override System.Web.UI.PageStatePersister PageStatePersister
        {
            get
            {
                return this.AcmsPageStatePersister;
            }
        }

        private Acms.StateManager.WebPageStatePersister psgPageStatePersister;
        /// <summary>
        /// Property PsgPageStatePersister
        /// </summary>
        public Acms.StateManager.WebPageStatePersister AcmsPageStatePersister
        {
            get
            {
                if (psgPageStatePersister == null)
                {
                    psgPageStatePersister = new Acms.StateManager.WebPageStatePersister(this);
                }
                return psgPageStatePersister;
            }
        }

        protected override System.Collections.Specialized.NameValueCollection DeterminePostBackMode()
        {
            return AcmsPageStatePersister.DeterminePostBackMode();
        }

        protected override object SaveViewState()
        {
            ViewState["Title"] = this.Title;
            return base.SaveViewState();
        }

        protected override void LoadViewState(object savedState)
        {
            base.LoadViewState(savedState);
            this.Title = ViewState["Title"] as string;
        }


    }
}
